package com.ruoyi.system.service.impl;

import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.entity.*;
import com.ruoyi.common.core.domain.model.LoginUser;
import com.ruoyi.common.exception.base.BaseException;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.system.mapper.*;
import com.ruoyi.system.service.*;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * 【请填写功能名称】Service业务层处理
 *
 * @author wlmes
 * @date 2023-11-23
 */
@Service
public class StorePanServiceImpl implements IStorePanService {
    @Resource
    private StorePanMapper storePanMapper;

    @Resource
    StorePanDetailMapper storePanDetailMapper;

    @Resource
    IStorePanDetailService storePanDetailService;

    @Resource
    RestaurantStoreResourceMapper restaurantStoreResourceMapper;

    @Resource
    RestaurantStoreResourceService restaurantStoreResourceService;

    @Resource
    IFoodStockService foodStockService;

    @Resource
    IRestaurantStoreResourceMoveService restaurantStoreResourceMoveService;

    @Resource
    StoreOutMapper storeOutMapper;

    @Resource
    StoreInMapper storeInMapper;

    @Resource
    StoreOutDetailMapper storeOutDetailMapper;

    @Resource
    StoreInDetailMapper storeInDetailMapper;

    @Resource
    IPanOverflowReportService panOverflowReportService;

    @Resource
    IPanOverflowReportDetailService panOverflowReportDetailService;

    @Resource
    private ApproveService approveService;

    /**
     * 查询【请填写功能名称】
     *
     * @param id 【请填写功能名称】ID
     * @return 【请填写功能名称】
     */
    @Override
    public StorePan selectStorePanById(Long id) {
        return storePanMapper.selectStorePanById(id);
    }

    /**
     * 查询【请填写功能名称】列表
     *
     * @param storePan 【请填写功能名称】
     * @return 【请填写功能名称】
     */
    @Override
    public List<StorePan> selectStorePanList(StorePan storePan) {
        return storePanMapper.selectStorePanList(storePan);
    }

    /**
     * 新增【请填写功能名称】
     *
     * @param storePan 【请填写功能名称】
     * @return 结果
     */
    @Override
    public int insertStorePan(StorePan storePan) {
        if (StringUtils.isNull(storePan)) {
            throw new BaseException("新增【请填写功能名称】失败！");
        }
        storePan.setCreateTime(DateUtils.getNowDate());
        LoginUser loginUser = SecurityUtils.getLoginUser();
        SysUser user = loginUser.getUser();
        storePan.setCreateBy(String.valueOf(user.getNickName()));
        return storePanMapper.insertStorePan(storePan);
    }

    /**
     * 修改【请填写功能名称】
     *
     * @param storePan 【请填写功能名称】
     * @return 结果
     */
    @Override
    public int updateStorePan(StorePan storePan) {
        storePan.setUpdateTime(DateUtils.getNowDate());
        LoginUser loginUser = SecurityUtils.getLoginUser();
        SysUser user = loginUser.getUser();
        storePan.setUpdateBy(String.valueOf(user.getNickName()));
        return storePanMapper.updateStorePan(storePan);
    }

    /**
     * 批量删除【请填写功能名称】
     *
     * @param ids 需要删除的【请填写功能名称】ID
     * @return 结果
     */
    @Override
    public int deleteStorePanByIds(Long[] ids) {
        if (StringUtils.isNull(ids)) {
            throw new BaseException("需要删除的【请填写功能名称】ID为空！");
        }
        return storePanMapper.deleteStorePanByIds(ids);
    }

    /**
     * 删除【请填写功能名称】信息
     *
     * @param id 【请填写功能名称】ID
     * @return 结果
     */
    @Override
    public int deleteStorePanById(Long id) {
        if (StringUtils.isNull(id)) {
            throw new BaseException("需要删除的【请填写功能名称】ID为空！");
        }
        return storePanMapper.deleteStorePanById(id);
    }

    @Override
    public StorePan getStoreDetailList(StorePan storePan) {

        List<StorePanDetail> storePanDetailList = new ArrayList<>();
        String area = area = storePan.getArea();
        ;
        if (storePan != null && StringUtils.isBlank(storePan.getArea())) {
            SysUser user = SecurityUtils.getLoginUser().getUser();
            area = user.getLocation();
        }
        if (StringUtils.isBlank(area)) {
            throw new BaseException("错误，登录人园区为空");

        }

        //当前日期
        Date today = new Date();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

        //计算盘点账期
        //当前月 日
        LocalDate localDate = LocalDate.now();
        int year = localDate.getYear();
        int month = localDate.getMonthValue();
        int day = localDate.getDayOfMonth();
        String panDate = "";
        String panDateStart = "";
        String panDateEnd = "";

        if (StringUtils.isNotBlank(storePan.getPanTime())) {
            panDate = storePan.getPanTime() + "-25";
            int a = Integer.valueOf(panDate.split("-")[1]);
            if (a == 1) {
                panDateStart = (Integer.valueOf(panDate.split("-")[0]) - 1) + "-12-26" + " 00:00:00";
            } else {
                panDateStart = Integer.valueOf(panDate.split("-")[0]) + "-" + (Integer.valueOf(panDate.split("-")[1]) - 1) + "-26" + " 00:00:00";
            }
            panDateEnd = panDate + " 23:59:59";
        } else {
            if (day - 25 <= 0) {
                //本月25
                panDate = month + "月" + "25日";
            } else {
                //下月25
                panDate = month + 1 == 13 ? "1" : month + 1 + "月" + "25日";
            }

            if (month == 1) {
                panDateEnd = year + "-" + month + "-" + "25" + " 23:59:59";//先赋值在减

                year = year - 1;
                month = 12;
                panDateStart = year + "-" + month + "-" + "26" + " 00:00:00";
            } else {
                panDateEnd = year + "-" + month + "-" + "25" + " 23:59:59";//先赋值在减

                panDateStart = year + "-" + (month - 1) + "-" + "26" + " 00:00:00";
            }
        }

        storePan.setPanTime(panDate);

        BigDecimal money = BigDecimal.ZERO;

        //获取所有库存信息
        FoodStock query = new FoodStock();
        query.setArea(area);
        List<FoodStock> restaurantStoreResourceList = foodStockService.selectRestaurantStoreResourceListByStock(query);
        for (FoodStock restaurantStoreResource : restaurantStoreResourceList) {
            StorePanDetail storePanDetail = new StorePanDetail();
            storePanDetail.setResourceId(restaurantStoreResource.getResourceId());
            storePanDetail.setArea(restaurantStoreResource.getArea());
            storePanDetail.setName(restaurantStoreResource.getName());
            storePanDetail.setUnit(restaurantStoreResource.getUnit());
            //按园区和食材查询全部的出入库记录
            BigDecimal out = BigDecimal.ZERO;
            List<StoreOutDetail> outDetails = new ArrayList<>();
            outDetails = storeOutDetailMapper.selectListByDate(panDateStart, panDateEnd, restaurantStoreResource.getResourceId());
//            if (flag) {
//                String panTime = sdf.format(lastPan.getPanTime());
//                String todayTime = sdf.format(today);
//                outDetails = storeOutDetailMapper.selectListByDate(panTime, todayTime, restaurantStoreResource.getResourceId());
//            } else {
//                StoreOutDetail store = new StoreOutDetail();
//                store.setResourceId(restaurantStoreResource.getResourceId());
//                outDetails = storeOutDetailMapper.selectListByConfirm(store);
//            }
            if (!CollectionUtils.isEmpty(outDetails)) {
                for (StoreOutDetail outDetail : outDetails) {
                    out = out.add(outDetail.getAmount() == null ? BigDecimal.ZERO : outDetail.getAmount());
                }
            }

            BigDecimal in = BigDecimal.ZERO;
            List<StoreInDetail> inDetails = new ArrayList<>();
            inDetails = storeInDetailMapper.selectListByDate(panDateStart, panDateEnd, restaurantStoreResource.getResourceId());
//            if (flag) {
//                String panTime = sdf.format(lastPan.getPanTime());
//                String todayTime = sdf.format(today);
//                inDetails = storeInDetailMapper.selectListByDate(panTime, todayTime, restaurantStoreResource.getResourceId());
//            } else {
//                StoreInDetail inStore = new StoreInDetail();
//                inStore.setResourceId(restaurantStoreResource.getResourceId());
//                inDetails = storeInDetailMapper.selectListByConfirm(inStore);
//            }
            if (!CollectionUtils.isEmpty(inDetails)) {
                for (StoreInDetail inDetail : inDetails) {
                    in = in.add(inDetail.getAmount() == null ? BigDecimal.ZERO : inDetail.getAmount());
                }
            }

            storePanDetail.setInAmount(in);
            storePanDetail.setOutAmount(out);
            storePanDetail.setLastAmount(restaurantStoreResource.getLastAmount() == null ? BigDecimal.ZERO : restaurantStoreResource.getLastAmount());//
            storePanDetail.setRealAmount(restaurantStoreResource.getAmount() == null ? BigDecimal.ZERO : restaurantStoreResource.getAmount());//实际库存先默认当前库存
            storePanDetail.setPrice(restaurantStoreResource.getAvgPrice() == null ? BigDecimal.ZERO : restaurantStoreResource.getAvgPrice());
            storePanDetail.setTotal(storePanDetail.getPrice().multiply(storePanDetail.getRealAmount()));

            //            storePanDetail.setStoreAmount(restaurantStoreResource.getAmount() == null ? BigDecimal.ZERO : restaurantStoreResource.getAmount());
            storePanDetail.setStoreAmount(storePanDetail.getLastAmount().add(storePanDetail.getInAmount()).subtract(storePanDetail.getOutAmount()));

            storePanDetailList.add(storePanDetail);

            money = money.add(storePanDetail.getRealAmount().multiply(storePanDetail.getPrice()));
        }

//        SysUser user = SecurityUtils.getLoginUser().getUser();
        storePan.setArea(area);
        storePan.setPanNum(BaseController.createBusinessNum("PDD"));
        storePan.setMoney(money);
        storePan.setStorePanDetailList(storePanDetailList);
        storePan.setApprovalState("");
        storePan.setPanTime(panDate);

        return storePan;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void saveNewStorePan(StorePan storePan) {
        if (StringUtils.isBlank(storePan.getArea())) {
            SysUser user = SecurityUtils.getLoginUser().getUser();
            storePan.setArea(user.getLocation());
        }

        if (StringUtils.isNotBlank(storePan.getArea()) && storePan.getArea().equals("全部")) {
            throw new BaseException("该登录用户园区为全部,不能发起");
        }

        List<StorePan> list = storePanMapper.selectStorePanListIng(storePan);
        if (!CollectionUtils.isEmpty(list)) {
            throw new BaseException("有未审批完的盘点单,请勿重新发起");
        }
        //判断重复
        StorePan pan = new StorePan();
        pan.setPanTime(storePan.getPanTime());
        List<StorePan> panList = storePanMapper.selectStorePanList(pan);
        if (!CollectionUtils.isEmpty(list)) {
            throw new BaseException("该盘点周期已有盘点单,请勿重新发起" + storePan.getPanTime());
        }

//        if (CollectionUtils.isEmpty(storePan.getStorePanDetailList())) {
//            throw new BaseException("保存失败,当前所有食材库存均为0");
//        }

        if (StringUtils.isNotBlank(storePan.getApprovalState()) && (storePan.getApprovalState().equals("1") || storePan.getApprovalState().equals("3"))) {
            throw new BaseException("盘点单在审核中或已通过审核,无法变更");
        }

        BigDecimal money = BigDecimal.ZERO;
        for (StorePanDetail storePanDetail : storePan.getStorePanDetailList()) {
            storePanDetail.setArea(storePan.getArea());
            storePanDetail.setPanNum(storePan.getPanNum());
            storePanDetailService.insertStorePanDetail(storePanDetail);
            BigDecimal price = storePanDetail.getPrice() == null ? BigDecimal.ZERO : storePanDetail.getPrice();
            BigDecimal num = storePanDetail.getRealAmount() == null ? BigDecimal.ZERO : storePanDetail.getRealAmount();
            money = money.add(price.multiply(num));
        }

        //保存盘点记录
        storePan.setMoney(money);
        storePan.setApprovalState("0");
        this.insertStorePan(storePan);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void approvePassStorePan(Long id) {
        if (id == null) {
            throw new BaseException("查询失败,参数为空");
        }
        StorePan storePan = storePanMapper.selectStorePanById(id);
        if (storePan == null) {
            throw new BaseException("查询失败,未找到对应的盘点单" + id);
        }
        storePan.setApprovalState("3");
        storePan.setApprovalTime(new Date());
        storePanMapper.updateStorePan(storePan);

        StorePanDetail query = new StorePanDetail();
        query.setPanNum(storePan.getPanNum());
        List<StorePanDetail> details = storePanDetailMapper.selectStorePanDetailList(query);

        List<StorePanDetail> more = new ArrayList<>();
        List<StorePanDetail> less = new ArrayList<>();

        for (StorePanDetail detail : details) {
            if (detail.getRealAmount().compareTo(detail.getStoreAmount()) == 1) {
                more.add(detail);
            }
            if (detail.getStoreAmount().compareTo(detail.getRealAmount()) == 1) {
                less.add(detail);
            }
        }

        //修改库存
        //记录库存修改明细
        //生成报溢和报损单
        if (!CollectionUtils.isEmpty(more)) {
            PanOverflowReport overflowReport = new PanOverflowReport();
            overflowReport.setArea(storePan.getArea());
            overflowReport.setBusinessNum(BaseController.createBusinessNum("BYD"));
            overflowReport.setStoreTime(new Date());
            overflowReport.setType("报溢单");
            List<PanOverflowReportDetail> detailList = new ArrayList<>();
            for (StorePanDetail storePanDetail : more) {
                PanOverflowReportDetail panOverflowReportDetail = new PanOverflowReportDetail();
                panOverflowReportDetail.setBusinessNum(overflowReport.getBusinessNum());
                panOverflowReportDetail.setType(overflowReport.getType());
                panOverflowReportDetail.setArea(overflowReport.getArea());
                panOverflowReportDetail.setResourceId(storePanDetail.getResourceId());
                panOverflowReportDetail.setAmount(storePanDetail.getRealAmount().subtract(storePanDetail.getStoreAmount()));
                panOverflowReportDetail.setUnit(storePanDetail.getUnit());
                detailList.add(panOverflowReportDetail);
                panOverflowReportDetailService.insertPanOverflowReportDetail(panOverflowReportDetail);

                //修改库存 记录明细
                updateStockByIdAndSaveRecord(storePan, storePanDetail, panOverflowReportDetail);
            }
            overflowReport.setDetailList(detailList);
            panOverflowReportService.insertPanOverflowReport(overflowReport);
        }

        //生成报溢和报损单
        if (!CollectionUtils.isEmpty(less)) {
            PanOverflowReport overflowReport = new PanOverflowReport();
            overflowReport.setArea(storePan.getArea());
            overflowReport.setBusinessNum(BaseController.createBusinessNum("BSD"));
            overflowReport.setStoreTime(new Date());
            overflowReport.setType("报损单");
            List<PanOverflowReportDetail> detailList = new ArrayList<>();
            for (StorePanDetail storePanDetail : less) {
                PanOverflowReportDetail panOverflowReportDetail = new PanOverflowReportDetail();
                panOverflowReportDetail.setBusinessNum(overflowReport.getBusinessNum());
                panOverflowReportDetail.setType(overflowReport.getType());
                panOverflowReportDetail.setArea(overflowReport.getArea());
                panOverflowReportDetail.setResourceId(storePanDetail.getResourceId());
                panOverflowReportDetail.setAmount(storePanDetail.getRealAmount().subtract(storePanDetail.getStoreAmount()));
                panOverflowReportDetail.setUnit(storePanDetail.getUnit());
                detailList.add(panOverflowReportDetail);
                panOverflowReportDetailService.insertPanOverflowReportDetail(panOverflowReportDetail);

                //修改库存 记录明细
                updateStockByIdAndSaveRecord(storePan, storePanDetail, panOverflowReportDetail);
            }
            overflowReport.setDetailList(detailList);
            panOverflowReportService.insertPanOverflowReport(overflowReport);
        }
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void approveRejectStorePan(Long id) {
        if (id == null) {
            throw new BaseException("查询失败,参数为空");
        }
        StorePan storePan = storePanMapper.selectStorePanById(id);
        if (storePan == null) {
            throw new BaseException("查询失败,未找到对应的盘点单" + id);
        }
        storePan.setApprovalState("2");
        storePan.setApprovalTime(new Date());
        storePanMapper.updateStorePan(storePan);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void confirmStorePan(StorePan old) {
        if (old == null || old.getId() == null) {
            throw new BaseException("查询失败,参数为空");
        }
        StorePan storePan = storePanMapper.selectStorePanById(old.getId());
        if (storePan == null) {
            throw new BaseException("查询失败,未找到对应的盘点单" + old.getId());
        }
        storePan.setApprovalState("1");
        storePan.setApprovalUser(old.getApprovalUser());
        storePan.setApprovalUserId(old.getApprovalUserId());
        storePanMapper.updateStorePan(storePan);

        //发起审批
        ApproveMission approveMission = ApproveMission.builder()
                .applicant(SecurityUtils.getUserId())
                .status("审批中")
                .name("盘点审批")
                .activityId(old.getId())
                .approver(old.getApprovalUserId())
                .activityType("盘点审批").build();
        approveMission.setCreateBy(SecurityUtils.getNickName());
        approveService.createApproveMission(approveMission);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public StorePan selectStorePanByPanNum(String panNum) {
        StorePan s = new StorePan();
        StorePan query = new StorePan();
        query.setPanNum(panNum);
        List<StorePan> storePans = storePanMapper.selectStorePanList(query);
        if (!CollectionUtils.isEmpty(storePans)) {
            s = storePans.get(0);
        }
        return s;
    }

    @Override
    public StorePanDetail getAmount(StorePanDetail detail) {
        StorePanDetail old = storePanDetailService.selectStorePanDetailById(detail.getId());
        StorePan storePan = this.selectStorePanByPanNum(old.getPanNum());

        List<StorePanDetail> storePanDetailList = new ArrayList<>();

        String area = detail.getArea();
        if (StringUtils.isBlank(area)) {
            SysUser user = SecurityUtils.getLoginUser().getUser();
            area = user.getLocation();
        }

        if (StringUtils.isBlank(area)) {
            throw new BaseException("错误，登录人园区为空");
        }
        if (detail.getResourceId() == null) {
            throw new BaseException("错误，新增明细时食材id不能为空");
        }

        //当前日期
        Date today = new Date();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

        //计算盘点账期
        //当前月 日
        LocalDate localDate = LocalDate.now();
        int year = localDate.getYear();
        int month = localDate.getMonthValue();
        int day = localDate.getDayOfMonth();
        String panDate = "";
        String panDateStart = "";
        String panDateEnd = "";

        if (StringUtils.isNotBlank(storePan.getPanTime())) {
            int a = Integer.valueOf(panDate.split("-")[1]);
            if (a == 1) {
                panDateStart = (Integer.valueOf(panDate.split("-")[0]) - 1) + "-12-26" + " 00:00:00";
            } else {
                panDateStart = Integer.valueOf(panDate.split("-")[0]) + "-" + (Integer.valueOf(panDate.split("-")[1]) - 1) + "-26" + " 00:00:00";
            }
            panDateEnd = panDate + " 23:59:59";
        } else {
            if (day > 25) {
                //本月25
                panDate = month + "月" + "25日";
            } else {
                //上月25
                panDate = month - 1 == 0 ? "12" : month - 1 + "月" + "25日";
            }

            if (month == 1) {
                panDateEnd = year + "-" + month + "-" + "25" + " 23:59:59";//先赋值在减

                year = year - 1;
                month = 12;
                panDateStart = year + "-" + month + "-" + "25" + " 00:00:00";
            } else {
                panDateEnd = year + "-" + month + "-" + "25" + " 23:59:59";//先赋值在减

                panDateStart = year + "-" + (month - 1) + "-" + "25" + " 00:00:00";
            }
        }

        //获取所有库存信息
        FoodStock query = new FoodStock();
        query.setArea(area);
        query.setResourceId(detail.getResourceId());
        List<FoodStock> restaurantStoreResourceList = foodStockService.selectRestaurantStoreResourceListByStockOne(query);
        for (FoodStock restaurantStoreResource : restaurantStoreResourceList) {
//            StorePanDetail storePanDetail = new StorePanDetail();
            detail.setResourceId(restaurantStoreResource.getResourceId());
            detail.setArea(restaurantStoreResource.getArea());
            detail.setName(restaurantStoreResource.getName());
            detail.setUnit(restaurantStoreResource.getUnit());
            //按园区和食材查询全部的出入库记录
            BigDecimal out = BigDecimal.ZERO;
            List<StoreOutDetail> outDetails = new ArrayList<>();
            outDetails = storeOutDetailMapper.selectListByDate(panDateStart, panDateEnd, restaurantStoreResource.getResourceId());
//            if (flag) {
//                String panTime = sdf.format(lastPan.getPanTime());
//                String todayTime = sdf.format(today);
//                outDetails = storeOutDetailMapper.selectListByDate(panTime, todayTime, restaurantStoreResource.getResourceId());
//            } else {
//                StoreOutDetail store = new StoreOutDetail();
//                store.setResourceId(restaurantStoreResource.getResourceId());
//                outDetails = storeOutDetailMapper.selectListByConfirm(store);
//            }
            if (!CollectionUtils.isEmpty(outDetails)) {
                for (StoreOutDetail outDetail : outDetails) {
                    out = out.add(outDetail.getAmount() == null ? BigDecimal.ZERO : outDetail.getAmount());
                }
            }

            BigDecimal in = BigDecimal.ZERO;
            List<StoreInDetail> inDetails = new ArrayList<>();
            inDetails = storeInDetailMapper.selectListByDate(panDateStart, panDateEnd, restaurantStoreResource.getResourceId());
//            if (flag) {
//                String panTime = sdf.format(lastPan.getPanTime());
//                String todayTime = sdf.format(today);
//                inDetails = storeInDetailMapper.selectListByDate(panTime, todayTime, restaurantStoreResource.getResourceId());
//            } else {
//                StoreInDetail inStore = new StoreInDetail();
//                inStore.setResourceId(restaurantStoreResource.getResourceId());
//                inDetails = storeInDetailMapper.selectListByConfirm(inStore);
//            }
            if (!CollectionUtils.isEmpty(inDetails)) {
                for (StoreInDetail inDetail : inDetails) {
                    in = in.add(inDetail.getAmount() == null ? BigDecimal.ZERO : inDetail.getAmount());
                }
            }

            detail.setInAmount(in);
            detail.setOutAmount(out);
            detail.setLastAmount(restaurantStoreResource.getLastAmount() == null ? BigDecimal.ZERO : restaurantStoreResource.getLastAmount());//
            detail.setRealAmount(restaurantStoreResource.getAmount() == null ? BigDecimal.ZERO : restaurantStoreResource.getAmount());//实际库存先默认当前库存
            detail.setPrice(restaurantStoreResource.getAvgPrice() == null ? BigDecimal.ZERO : restaurantStoreResource.getAvgPrice());
            detail.setTotal(detail.getPrice().multiply(detail.getRealAmount()));

//            detail.setStoreAmount(restaurantStoreResource.getAmount() == null ? BigDecimal.ZERO : restaurantStoreResource.getAmount());
            detail.setStoreAmount(detail.getLastAmount().add(detail.getInAmount()).subtract(detail.getOutAmount()));

            storePanDetailList.add(detail);
        }

//        SysUser user = SecurityUtils.getLoginUser().getUser();
//        if (CollectionUtils.isEmpty(storePanDetailList)) {
//            throw new BaseException("未找到食材");
//        }

        return detail;
    }

    @Transactional(rollbackFor = Exception.class)
    public void updateStockByIdAndSaveRecord(StorePan storePan, StorePanDetail storePanDetail, PanOverflowReportDetail panOverflowReportDetail) {
        //修改库存
        foodStockService.updateStockById(storePan.getArea(), panOverflowReportDetail.getResourceId(), storePanDetail.getRealAmount(), storePanDetail.getStoreAmount());
        //记录移动明细
        RestaurantStoreResourceMove move = new RestaurantStoreResourceMove();
        move.setAmount(panOverflowReportDetail.getAmount());
        move.setResourceId(panOverflowReportDetail.getResourceId());
        move.setBusinessNum(panOverflowReportDetail.getBusinessNum());
        move.setBusinessType(panOverflowReportDetail.getType());
        move.setUnit(panOverflowReportDetail.getUnit());
        move.setArea(storePan.getArea());
        restaurantStoreResourceMoveService.insertRestaurantStoreResourceMove(move);
    }
}
